package com.aier.common.entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

public class MessageRowMapper implements RowMapper<Message> {

    @SuppressWarnings("unused")
    private int type;

    public MessageRowMapper() {
    }

    public MessageRowMapper(int type) {
        this.type = type;
    }

    @Override
    public Message mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseCountry(new Message(), rs);
    }

    public static Message parseCountry(Message m, ResultSet rs)
        throws SQLException {
        return parseCountry(m, rs, null);
    }

    public static Message parseCountry(Message m, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            m.setMid(rs.getLong(su + "mid"));
        } catch (SQLException e) {
            m.setMid(null);
        }
        try {
            m.setPhone(rs.getString(su + "phone"));
        } catch (SQLException e) {
            m.setPhone(null);
        }
        try {
            String str = rs.getString(su+"content");
            if (str != null) {
                str = str.replaceAll("<", "&lt;");
                str = str.replaceAll(">", "&gt;");
            }
            m.setContent(str);
        } catch (SQLException e) {
            m.setContent(null);
        }
        return m;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, Message m) {
        par.put("mid", m.getMid());
        par.put("phone", m.getPhone());
        par.put("content", m.getContent());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            Message m) {
        args.put("mid", m.getMid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final Message m) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(m),
                        new String[] { "mid" });
                parsePS(ps, m);
                return ps;
            }

        };
    }

    private static String parseSQL(Message m) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into message(");
        int count = 0;
        if (null != m.getMid()) {
            sql.append("mid,");
            count++;
        }

        if (null != m.getPhone()) {
            sql.append("phone,");
            count++;
        }

        if (null != m.getContent()) {
            sql.append("content,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, Message m)
        throws SQLException {
        int count = 1;
        if (null != m.getMid()) {
            ps.setLong(count++, m.getMid());
        }

        if (null != m.getPhone()) {
            ps.setString(count++, m.getPhone());
        }

        if (null != m.getContent()) {
            ps.setString(count++, m.getContent());
        }

    }

}
